// src/permissions/permissions.controller.ts
import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  NotFoundException,
} from '@nestjs/common';
import { PermissionService } from './permission.service';
import { CreatePermissionDto } from './dto/create-permission.dto';
import { UpdatePermissionDto } from './dto/update-permission.dto';
import { Permission } from './entities/permission.entity';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiParam,
  ApiBody,
} from '@nestjs/swagger';

@ApiTags('权限管理') // 为控制器添加标签
@Controller('permission')
export class PermissionController {
  constructor(private readonly permissionService: PermissionService) {}

  // 创建单个权限
  @Post()
  @ApiOperation({ summary: '创建权限' })
  @ApiBody({ type: CreatePermissionDto, description: '创建权限所需数据' })
  @ApiResponse({ status: 201, description: '权限创建成功', type: Permission })
  async create(
    @Body() createPermissionDto: CreatePermissionDto,
  ): Promise<Permission> {
    return await this.permissionService.create(createPermissionDto);
  }

  // 批量创建权限
  @Post('batch')
  @ApiOperation({ summary: '批量创建权限' })
  @ApiBody({
    type: [CreatePermissionDto],
    description: '批量创建权限所需数据（权限数组）',
  })
  @ApiResponse({
    status: 201,
    description: '权限批量创建成功',
    type: [Permission],
  })
  async batchCreate(
    @Body() createPermissionDtos: CreatePermissionDto[],
  ): Promise<Permission[]> {
    const permissions = [];
    for (const dto of createPermissionDtos) {
      const permission = await this.permissionService.create(dto);
      permissions.push(permission);
    }
    return permissions;
  }

  @Post('import')
  @ApiOperation({ summary: '一键导入权限' })
  async importPermissions() {
    return this.permissionService.importPermissions();
  }

  /**
   * 获取所有权限
   */
  @Get()
  @ApiOperation({ summary: '获取所有权限' })
  @ApiResponse({
    status: 200,
    description: '返回所有权限列表',
    type: [Permission],
  })
  async findAll(): Promise<Permission[]> {
    return await this.permissionService.findAllPermissionsWithNested();
  }

  /**
   * 根据ID查询单个权限
   */
  @Get(':id')
  @ApiOperation({ summary: '根据ID查询单个权限' })
  @ApiParam({ name: 'id', type: 'number', description: '权限ID' })
  @ApiResponse({
    status: 200,
    description: '返回指定ID的权限',
    type: Permission,
  })
  @ApiResponse({ status: 404, description: '权限未找到' })
  async findOne(@Param('id') id: string): Promise<Permission> {
    const permission = await this.permissionService.findOne(id);
    if (!permission) {
      throw new NotFoundException(`权限 ID ${id} 未找到`);
    }
    return permission;
  }

  /**
   * 更新权限
   */
  @Patch(':id')
  @ApiOperation({ summary: '更新权限' })
  @ApiParam({ name: 'id', type: 'number', description: '权限ID' })
  @ApiBody({ type: UpdatePermissionDto, description: '更新权限所需数据' })
  @ApiResponse({ status: 200, description: '权限更新成功', type: Permission })
  @ApiResponse({ status: 404, description: '权限未找到' })
  async update(
    @Param('id') id: string,
    @Body() updatePermissionDto: UpdatePermissionDto,
  ): Promise<Permission> {
    return await this.permissionService.update(id, updatePermissionDto);
  }

  /**
   * 逻辑删除权限
   */
  @Delete(':id')
  @ApiOperation({ summary: '逻辑删除权限' })
  @ApiParam({ name: 'id', type: 'number', description: '权限ID' })
  @ApiResponse({ status: 200, description: '权限逻辑删除成功' })
  @ApiResponse({ status: 404, description: '权限未找到' })
  async remove(@Param('id') id: string): Promise<void> {
    await this.permissionService.remove(id);
  }

  /**
   * 永久删除权限
   */
  @Delete('hard-delete/:id')
  @ApiOperation({ summary: '永久删除权限' })
  @ApiParam({ name: 'id', type: 'number', description: '权限ID' })
  @ApiResponse({ status: 200, description: '权限永久删除成功' })
  @ApiResponse({ status: 404, description: '权限未找到' })
  async hardDelete(@Param('id') id: string): Promise<void> {
    await this.permissionService.hardDelete(id);
  }
}
